package com.my.javaweb;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*****
 * service和doGet/doPost方法同时存在service优先
 * get方式乱码:
 * Tomcat-conf-server.xml设置URIEncoding="UTF-8"
 * 或者：new String (suser.getBytes(“ISO-8859-1”),”UTF-8”)
 *
 * post方式乱码:
 * request.setCharacterEncoding(“UTF-8”);
 *
 * response:
 * response.setCharacterEncoding(“UTF-8");
 * response.setContentType("text/html; charset=UTF-8");
 *
 * */
@WebServlet(name = "servlet1" ,urlPatterns = "/ser1")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是service方法");
        //用于解决post方法中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        //获取参数
        String name = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        System.out.println("service-----" + name + "------" + pwd);

        //super.service(req, resp);既然重写就不要保留介个
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        //获取参数
        String name = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        System.out.println("doGet-----"+name + "------" + pwd);
        System.out.println("我是doGet方法");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //用于解决post方法中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        //获取参数
        String name = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        System.out.println("doPost-----"+name + "------" + pwd);
        System.out.println("我是doPost方法");
    }
}
